{-# LANGUAGE ViewPatterns #-}
-----------------------------------------------------------------------------
--
-- Module      :  LLVM.Code.Instructions.Cast
-- Copyright   :  Mikhail Belyaev
-- License     :  GPL (Just (Version {versionBranch = [3], versionTags = []}))
--
-- Maintainer  :
-- Stability   :
-- Portability :
--
-- |
--
-----------------------------------------------------------------------------

module LLVM.Code.Instructions.Cast where

import Data.Maybe (fromJust)
import Data.Maybe (isJust)
import LLVM.Parser.Record.Record (recordAsList)
import LLVM.Parser.Record.Record (RecordType(..))
import LLVM.Util
import LLVM.Code.Shared (ValIndex(..))
import LLVM.Code.Shared (TypeIndex(..))
import LLVM.Code.Shared (CastOpcode)
import LLVM.Code.Instructions.Class (BuildableInstruction(..))
import LLVM.Code.Instructions.Util
       (Matched(..), Matcher(..), matchRRwithCode)
import LLVM.Code.Types (Type)
import LLVM.Code.Instructions.Values (CastInstruction(..))




instance BuildableInstruction CastInstruction where
    buildInstruction rr tt vt =
        case (matchRRwithCode tt vt [MValueTypePair, MType, MInteger] 3 rr) of
             [MdValueTypePair (v,rty), MdType sty, MdInteger (integerToIC -> Just bopc)] ->
                   Just CastInstruction {
                            ciOpcode  = bopc,
                            ciResType = rty,
                            ciOpType  = sty,
                            ciOp      = v
                   }
             _ -> Nothing


